/*
 * @Description: 
 * @FilePath: /STM32/USER/bsp/rcc.c
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2021-04-28 15:25:19
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-04-28 22:01:26
 * @Copyright: Copyright  2017 muyiro. All rights reserved.
 */

#include "rcc.h"

void func_rcc_periph_clock_enable(uint32_t dev_base, uint32_t rcc_periph)
{
    uint32_t base = dev_base & 0xFFFF0000;
    
    if      (base == APB1PERIPH_BASE) { RCC_APB1PeriphClockCmd(rcc_periph, ENABLE); return; }
    else if (base == APB2PERIPH_BASE) { RCC_APB2PeriphClockCmd(rcc_periph, ENABLE); return; }
    else if (base == AHB1PERIPH_BASE) { RCC_AHB1PeriphClockCmd(rcc_periph, ENABLE); return; }
    else if (base == AHB2PERIPH_BASE) { RCC_AHB2PeriphClockCmd(rcc_periph, ENABLE); return; }
    assert_param(0); // error
}

uint32_t func_rcc_periph_clock_rate(uint32_t dev_base)
{
    uint32_t base = dev_base & 0xFFFF0000;
    RCC_ClocksTypeDef RCC_Clocks;
    RCC_GetClocksFreq(&RCC_Clocks);
    
    if      (base == APB1PERIPH_BASE) { return RCC_Clocks.PCLK1_Frequency / 1000000 * 2; }
    else if (base == APB2PERIPH_BASE) { return RCC_Clocks.PCLK2_Frequency / 1000000 * 2; }
    else if (base == AHB1PERIPH_BASE) { return RCC_Clocks.HCLK_Frequency  / 1000000; }
    else if (base == AHB2PERIPH_BASE) { return RCC_Clocks.HCLK_Frequency  / 1000000; }
    assert_param(0); // error
    return 168;
}
